package com.student.Activitys.activity;

import android.content.Intent;

import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseConversationListFragment;
import com.student.Activitys.R;
import com.student.Activitys.activity.base.BaseActivity;
import com.student.Activitys.listener.MyConnectionListener;
import com.student.Activitys.ring_letter.activity.ChatActivity;

import java.util.List;

/**
 * 项目名称：
 * 类描述：会话
 * 创建人：张飞祥
 * 创建时间：2017/9/18 11:15
 * 修改人：张飞祥
 * 修改时间：2017/9/18 11:15
 * 修改备注：
 */
public class ConversationActivity extends BaseActivity {

    //环信 消息 + 联系人监听
    private static EMMessageListener emMessageListener;
    //会话
    EaseConversationListFragment conversationListFragment;
    @Override
    protected void initUI() {
        setContentView(R.layout.act_public_add_fragment);
    }

    @Override
    protected void initData() {
        //环信 消息监听
        initContactsMonitor();
        conversationListFragment = new EaseConversationListFragment();
        //消息条目点击事件
        conversationListFragment.setConversationListItemClickListener(new EaseConversationListFragment.EaseConversationListItemClickListener() {
            @Override
            public void onListItemClicked(EMConversation conversation) {
                //进入聊天页面
                startActivity(new Intent(ConversationActivity.this, ChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, conversation.getUserName()));
            }
        });
        getSupportFragmentManager().beginTransaction().add(R.id.layout_add_fg, conversationListFragment).commit();
    }

    @Override
    protected void getNotData() {

    }

    /**
     * 环信 监听方法
     */
    private void initContactsMonitor() {
        //注册一个监听连接状态的listener
        EMClient.getInstance().addConnectionListener(new MyConnectionListener(this));
        emMessageListener = new EMMessageListener() {

            @Override
            public void onMessageReceived(List<EMMessage> messages) {
                //收到消息----刷新一下当前页面喽
                conversationListFragment.refresh();
                EMClient.getInstance().chatManager().importMessages(messages);//保存到数据库
            }

            @Override
            public void onCmdMessageReceived(List<EMMessage> messages) {
                //收到透传消息
            }

            @Override
            public void onMessageReadAckReceived(List<EMMessage> messages) {
                //收到已读回执
            }

            @Override
            public void onMessageDeliveryAckReceived(List<EMMessage> message) {
                //收到已送达回执
            }

            @Override
            public void onMessageChanged(EMMessage message, Object change) {
                //消息状态变动
            }
        };
        EMClient.getInstance().chatManager().addMessageListener(emMessageListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EMClient.getInstance().chatManager().removeMessageListener(emMessageListener);
    }
}
